﻿Imports ComponentFactory.Krypton.Toolkit
Imports System.Data.OleDb


Public Class consultaCaixa
    Inherits KryptonForm
    Private conexao As OleDbConnection
    Private command As OleDbCommand
    Private cmd As OleDbCommand
    Private ds As DataSet
    Private dv, dv1 As DataView
    Private dataAdapter As OleDbDataAdapter
    Private dt As OleDbDataAdapter
    Dim strQueryEntradaCaixa As String = "SELECT codentradaCaixa,valorentradaCaixa as Valor, motivoentradaCaixa as Motivo, dtentradaCaixa as Data FROM tblEntradaCaixa  "
    Dim strQuerySaidaCaixa As String = "SELECT codsaidaCaixa,valorsaidaCaixa as Valor, motivosaidaCaixa as Motivo, dtsaidaCaixa as Data, tblLogin.usuarioLog as Usuario FROM tblSaidaCaixa INNER JOIN tblLogin ON tblLogin.codLogin = tblSaidaCaixa.codLogin"

#Region "Atributos"
    Dim dtinicioEntrada As Date
    Dim dtfinalEntrada As Date
    Dim dtinicioSaida As Date
    Dim dtfinalSaida As Date
#End Region

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

        conexao = FabricaDeConexoes.CriarConexao()
        carregarDTG()

    End Sub

    Private Sub carregarDTG()
        Dim command As New OleDbCommand(strQueryEntradaCaixa, conexao)
        Dim cmd As New OleDbCommand(strQuerySaidaCaixa, conexao)
        dt = New OleDbDataAdapter(cmd)
        dataAdapter = New OleDbDataAdapter(command)
        ds = New DataSet()

        Try
            If (ds IsNot Nothing) Then

                dataAdapter.Fill(ds, "tblEntradaCaixa")
                dv = New DataView(ds.Tables("tblEntradaCaixa"), Nothing, "Valor", DataViewRowState.CurrentRows)
                dtgconentradacaixa.DataSource = dv
                dtgconentradacaixa.Columns(0).Visible = False
                dtgconentradacaixa.Columns(1).DefaultCellStyle.Format = "C"
            End If

            dt.Fill(ds, "tblSaidaCaixa")
            dv1 = New DataView(ds.Tables("tblSaidaCaixa"), Nothing, "valor", DataViewRowState.CurrentRows)
            dtgconsaidacaixa.DataSource = dv1
            dtgconsaidacaixa.Columns(0).Visible = False
            dtgconsaidacaixa.Columns(1).DefaultCellStyle.Format = "C"

        Catch ex As Exception
            Debug.WriteLine(ex)
        Finally
            dt.Dispose()
        End Try
    End Sub
    Private Sub filtrarSaida()
        If (txtInicioSaida.Value <= txtFinalSaida.Value) Then
            Dim dtinicioSaida = txtInicioSaida.Value.ToString("MM/dd/yyyy")
            Dim dtfinalSaida = txtFinalSaida.Value.ToString("MM/dd/yyyy")
            dv1.RowFilter = "Data >= #" & dtinicioSaida & "# AND Data <= #" & dtfinalSaida & "#"
        ElseIf (txtInicioSaida.Value > txtFinalSaida.Value) Then
            KryptonMessageBox.Show("A data de Inicio deve ser anterior a de Final", "Erro na Consulta!")
        End If
    End Sub
    Private Sub filtrarEntrada()
        If (txtInicioEntrada.Value <= txtFinalEntrada.Value) Then
            Dim dtinicioEntrada = txtInicioEntrada.Value.ToString("MM/dd/yyyy")
            Dim dtfinalEntrada = txtFinalEntrada.Value.ToString("MM/dd/yyyy")
            dv.RowFilter = "Data >= #" & dtinicioEntrada & "# AND Data <= #" & dtfinalEntrada & "#"
        ElseIf (txtInicioEntrada.Value > txtFinalEntrada.Value) Then
            KryptonMessageBox.Show("A data de Inicio deve ser anterior a de Final", "Erro na Consulta!")
        End If
    End Sub

    Private Sub btnpesquisarsaida_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpesquisarsaida.Click
        filtrarSaida()
    End Sub

    Private Sub txtfechar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtfechar.Click
        Me.Dispose()
    End Sub

    Private Sub btnlimparsaida_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlimparsaida.Click
        txtInicioSaida.Value = "1/1/2010"
        txtFinalSaida.Value = DateTime.Now.ToString("dd/MM/yyyy")
        carregarDTG()
    End Sub

    Private Sub btnlimparentrada_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlimparentrada.Click
        txtInicioEntrada.Value = "1/1/2010"
        txtFinalEntrada.Value = DateTime.Now.ToString("dd/MM/yyyy")
        carregarDTG()
    End Sub

    Private Sub btnpesquisarentrada_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpesquisarentrada.Click
        filtrarEntrada()
    End Sub

    Private Sub btnimprimirEntrada_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimprimirEntrada.Click
        Dim str As String = String.Format("SELECT tblCliente.nomeCliente, tblEntradaCaixa.valorentradaCaixa, tblEntradaCaixa.motivoEntradaCaixa, tblEntradaCaixa.dtEntradaCaixa " & _
            "FROM (tblOrdemServico INNER JOIN (tblCliente INNER JOIN tblAuto ON tblCliente.codCliente = tblAuto.codCliente) ON tblOrdemServico.codAuto = tblAuto.codAuto) INNER JOIN tblEntradaCaixa ON tblOrdemServico.codordemServico = tblEntradaCaixa.codordemServico WHERE dtEntradaCaixa BETWEEN #{0}# AND #{1}#", txtInicioEntrada.Value.ToString("MM/dd/yyyy"), txtFinalEntrada.Value.ToString("MM/dd/yyyy"))

        Dim doc = Config.DiretorioAplicacao & "/../../../Relatorio/Entradas.rpt"
        VisualizadorRelatorio.gerarRelatorio(str, "tblEntradaCaixa", doc)
    End Sub

    Private Sub btnimprimirSaida_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimprimirSaida.Click
        Dim str As String = String.Format("SELECT tblSaidaCaixa.valorsaidaCaixa, tblSaidaCaixa.motivosaidaCaixa, tblLogin.usuarioLog, tblSaidaCaixa.dtsaidaCaixa " & _
            "FROM tblLogin INNER JOIN tblSaidaCaixa ON tblLogin.codLogin = tblSaidaCaixa.codLogin WHERE dtSaidaCaixa BETWEEN #{0}# AND #{1}#", txtInicioSaida.Value.ToString("MM/dd/yyyy"), txtFinalSaida.Value.ToString("MM/dd/yyyy"))

        Dim doc = Config.DiretorioAplicacao & "/../../../Relatorio/Saidas.rpt"
        VisualizadorRelatorio.gerarRelatorio(str, "tblSaidaCaixa", doc)
    End Sub
End Class